/**
 * 
 */
package org.json.simple;

import java.io.IOException;
import java.io.Writer;

/**
 * @author Roger Xu
 *
 */
public class JSONWriter<T> extends Writer {

    private T writer = null;

    public JSONWriter(T writer) {
        this.writer = writer;
    }

    @Override
    public void write(char[] cbuf, int off, int len) throws IOException {
        if (writer instanceof Writer) {
            ((Writer) writer).write(cbuf, off, len);
        } else if (writer instanceof StringBuilder) {
            ((StringBuilder) writer).append(cbuf, off, len);
        } else if (writer instanceof StringBuffer) {
            ((StringBuffer) writer).append(cbuf, off, len);
        }
    }

    @Override
    public void flush() throws IOException {
        if (writer instanceof Writer) {
            ((Writer) writer).flush();
        }
    }

    @Override
    public void close() throws IOException {
        if (writer instanceof Writer) {
            ((Writer) writer).close();
        }
    }

}
